/* This file pools all Dartmouth Atlas zipcode-hsa-hrr files into a single pool.*  Dartmouth Atlas files 1995-2010 downloaded from http://www.dartmouthatlas.org/tools/downloads.aspx#crosswalks on 4/5/2012*/clearcap clear matrixset more offset linesize 150set matsize 800* Set the base folder for the Dartmouth Atlas zipcode-hsa-hrr fileslocal base PATH_TO_PROJECT_HOME_DIRECTORY/data/crosswalks/zip_hrr* ----------------------------------------------------------------------------------------------------------------------------* Step 1. Convert .xls files to .dta files (using stat transfer)* Input:		`base'/xls/ziphsahrr`yr'.xls* Output:	`base'/dta/ziphsahrr`yr'.dta* ----------------------------------------------------------------------------------------------------------------------------foreach yr in 95 96 97 98 99 00 01 02 03 04 05 06 07 08 09 10 {	shell st `base'/xls/ziphsahrr`yr'.xls `base'/dta/ziphsahrr`yr'.dta -y}* ----------------------------------------------------------------------------------------------------------------------------* Step 2. Pool all cw years together.* Input:		`base'/dta/ziphsahrr`yr'.dta* Output:	`base'/ziphsahrr_95_10.dta* ----------------------------------------------------------------------------------------------------------------------------if 1 {local appendfilesforeach yr in 95 96 97 98 99 00 01 02 03 04 05 06 07 08 09 10 {	* Load the crosswalk for year `yr'	use `base'/dta/ziphsahrr`yr', clear		* Rename the zipcode variable	rename zipcode`yr' zipcode		* Add year variable	if 95<=`yr' & `yr'<=99 gen zyear = 19`yr'	if 00<=`yr' & `yr'<=10 gen zyear = 20`yr'		* Save temporary copy	saveold `base'/ziphsahrr`yr'_tmp.dta, replace		* Add file to list of files to append	local appendfiles `appendfiles' `base'/ziphsahrr`yr'_tmp}clearappend using `appendfiles'order zipcode zyearsort zipcode zyearsaveold `base'/ziphsahrr_95_10, replace* Cleanup temporary filesforeach yr in 95 96 97 98 99 00 01 02 03 04 05 06 07 08 09 10 {	cap rm `base'/ziphsahrr`yr'_tmp.dta}}* ----------------------------------------------------------------------------------------------------------------------------* Step 3. Create expanded zip-to-HRR crosswalk file, that fills in missing years: *	Fill method: first copies non-missing values forward (within zipcode), then copies non-missing backward* Input:		`base'/ziphsahrr_95_10.dta* Output:	`base'/ziphsahrr_95_10_filled.dta* ----------------------------------------------------------------------------------------------------------------------------if 1 {* Create a masterfile of all zips that ever appear in the 1995-2010 Atlas crosswalks, with one obs per zip-yearuse zipcode using `base'/ziphsahrr_95_10, clearbys zipcode: keep if _n==1expand 16bys zipcode: gen zyear = 1994 + _nsort zipcode zyear* Merge in original cross walk data on (zipcode zyear).  Gaps in the original crosswalk will have missing valuesmerge 1:1 zipcode zyear using `base'/ziphsahrr_95_10, assert(master match) nogenerate noreport* Copy non-missing values forward: see FAQ at http://www.stata.com/support/faqs/data/missing.htmlsort zipcode zyeargen imputed = missing(hrrnum)foreach var of varlist hsanum hsacity hsastate hrrnum hrrcity hrrstate {	by zipcode: replace `var' = `var'[_n-1] if missing(`var')}* Copy non-missing values backward (for zips that start out with missing data)gsort zipcode -zyearforeach var of varlist hsanum hsacity hsastate hrrnum hrrcity hrrstate {	by zipcode: replace `var' = `var'[_n-1] if missing(`var')}sort zipcode zyear* Savesaveold `base'/ziphsahrr_95_10_filled, replace}